'''
Created on July 07, 2011

@author: yc30rr1
'''
import MimeWriter
import mimetools
import cStringIO
import smtplib
from library.AutoLoad import AutoLoad
log = getLogger(__name__)
class EmailSending(object):
    def __init__(self):
        try:
            
            self._confReader=AutoLoad.ClassObject('library.confiquration')
        except:
            AutoLoad.logExceptionInfo()
        
    
    def createhtmlmail (self,html,subject,sender,recipient):
        out = cStringIO.StringIO() # output buffer for our message 
        htmlin = cStringIO.StringIO(html)
              
        writer = MimeWriter.MimeWriter(out)
        #
        # set up some basic headers... we put subject here
        # because smtplib.sendmail expects it to be in the
        # message body
        #
        writer.addheader("Subject", subject)
        writer.addheader("MIME-Version", "1.0")
        writer.addheader("From", sender)
        writer.addheader("To", recipient)
    
        #
        # start the multipart section of the message
        # multipart/alternative seems to work better
        # on some MUAs than multipart/mixed
        #
        writer.startmultipartbody("alternative")
        writer.flushheaders()
        #
        # the plain text section
        #
        subpart = writer.nextpart()
        subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
        pout = subpart.startbody("text/plain", [("charset", 'us-ascii')])
       # mimetools.encode(txtin, pout, 'quoted-printable')
       # txtin.close()
        #
        # start the html subpart of the message
        #
        subpart = writer.nextpart()
        subpart.addheader("Content-Transfer-Encoding", "quoted-printable")
        #
        # returns us a file-ish object we can write to
        #
        pout = subpart.startbody("text/html", [("charset", 'us-ascii')])
        mimetools.encode(htmlin, pout, 'quoted-printable')
        htmlin.close()
        #
        # Now that we're done, close our writer and
        # return the message body
        #
        writer.lastpart()
        msg = out.getvalue()
        out.close()
        return msg
    def getEmailSetup(self):
#        _configReader=AutoLoad.ClassObject('library.confiquration')
        self.sender=self.__confReader.getOptionFromSection('emailsetup', 'from')
        self.recipient=self.__confReader.getOptionFromSection('emailsetup', 'to')

    def getLatestFileFromLocation(self):
        self.directoryLocation=self.__confReader.getOptionFromSection('testcases_html', 'testresults_location')
        filelist = os.listdir(self.directoryLocation)
        filelist = filter(lambda x: not os.path.isdir(x), filelist)
        newest = max(filelist, key=lambda x: os.stat(x).st_mtime)
        return newest

    def sendMail(self):
        htmlfile=self.getLatestFileFromLocation()
#        htmlfile='E:\GPython\csvtodbcompare\TestResults\Results2011-07-07 15_58_18.999000.html'
        f = open(htmlfile, 'r')
        html = f.read()
        f.close()
        subject = "Macys Stella Automation Team!"
        self.getEmailSetup()
#        self.sender='ramesh.ramaiyan@aspiresys.com'
#        self.recipient='omraj.krishnamurthi@aspiresys.com'
        message =self.createhtmlmail(html,subject,self.sender,self.recipient)
        server = smtplib.SMTP("mail.aspiresys.com")
        server.sendmail(self.sender,self.recipient, message)
        server.quit()

    

if __name__=="__main__":
    
    obj=EmailSending()

    obj.sendMail()
#    
#    f = open("E:\GPython\csvtodbcompare\TestResults\Results2011-07-07 15_58_18.999000.html", 'r')
#    html = f.read()
#    f.close()
#    subject = "Macys Stella Automation Team!"
#    sender='ramesh.ramaiyan@aspiresys.com'
#    recipient='omraj.krishnamurthi@aspiresys.com'
#    message =obj.createhtmlmail(html,subject,sender,recipient)
#    server = smtplib.SMTP("mail.aspiresys.com")
#    server.sendmail(sender, recipient, message)
#    server.quit()